################################################
## 						Hyeok Yong Kwon                                     ##
##                          Korea University                                    ##
##                         hkwon@korea.ac.kr                                ##
##                  Replication code for graphics in                     ##
## "Government Partisanship and Electoral Accountability ##
################################################

## Note: Run Stata code first

###############
#       Figure 1      #
###############

par(mfrow=c(1,2))

plot(rep(1:2), axes=FALSE, ylim=c(0,1), main='Non-Left voters (t-1)', ylab='Left Vote (t)', xlab='Employment', type='n')
axis(1, at=1:2, lab=c("Good", "Bad"))

box()
points(1:2, c(0.2, 0.7), type='b', lty=1, lwd=3, cex=2) #Rightgovt
points(1:2, c(0.2, 0.1), type='b', lty=2, lwd=3, cex=2) #Leftgovt 
text(locator(2), c('Right govt', 'Left govt'))

plot(rep(1:2), axes=FALSE, ylim=c(0,1), main='Left voters (t-1)', ylab='Left Vote (t)', xlab='Employment', type='n')
axis(1, at=1:2, lab=c("Good", "Bad"))

box()
points(1:2, c(0.7,0.7), type='b', col='red', lty=1, lwd=3, cex=2) #Rightgovt
points(1:2, c(0.8,0.6),type='b', col='red', lty=2, lwd=3, cex=2) #Leftgovt 
text(locator(2), c('Right govt', 'Left govt'))


###############
#       Figure 2      #
###############

# Run Stata code first 

library(networkD3)

# Panel (a) 1992-97

nodes = data.frame("name" = 
	c("Cons 92", "Labour 92", "Lib Dem 92", "Abstention 92", "Cons 97", "Labour 97", "Lib Dem 97", "Abstention 97"))

links = as.data.frame(matrix(c(
	0, 4, 419, 
	0, 5, 95,
	0, 6, 87, 
	0, 7, 81,
	1, 4, 9,
	1, 5, 479,
	1, 6, 23,
	1, 7, 52,
	2, 4, 16,
	2, 5, 96,
	2, 6, 159,
	2, 7, 21,
	3, 4, 27,
	3, 5, 41, 
	3, 6, 16,
	3, 7, 68), # first row source, second row target, third row value
	byrow=TRUE, ncol=2))

names(links) = c("source", "target", "value")

sankeyNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", fontSize=12, nodeWidth=25)

# Panel (b) 1997-2001

nodes = data.frame("name" = 
	c("Cons 97", "Labour 97", "Lib Dem 97", "Abstention 97", "Cons 01", "Labour 01", "Lib Dem 01", "Abstention 01"))

links = as.data.frame(matrix(c(
	0, 4, 365, 
	0, 5, 38,
	0, 6, 51, 
	0, 7, 76,
	1, 4, 33,
	1, 5, 640,
	1, 6, 71,
	1, 7, 162,
	2, 4, 38,
	2, 5, 43,
	2, 6, 190,
	2, 7, 50,
	3, 4, 52,
	3, 5, 77, 
	3, 6, 34,
	3, 7, 230), # first row source, second row target, third row value
	byrow=TRUE, ncol=3))

names(links) = c("source", "target", "value")

sankeyNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", fontSize=12, lineWidth=25)


###############
#       Figure 3      #
###############

dist <- read.csv("dist.csv", na.strings='.', header=T);

attach(dist)

par(mfrow=c(2,3))

barplot(dif92, main='1992', xlab='Employment Insecurity', ylab='Differences between Labour vs non-Labour Vote', axes=F, ylim=c(-80,60))
axis(1, at=1:5, labels=c(1,2,3,4,5))
axis(2) 

barplot(dif94, main='1994', xlab='Employment Insecurity', ylab='Differences between Labour vs non-Labour Vote', axes=F, ylim=c(-80,60))
axis(1, at=1:5, labels=c(1,2,3,4,5))
axis(2) 

barplot(dif95, main='1995', xlab='Employment Insecurity', ylab='Differences between Labour vs non-Labour Vote', axes=F, ylim=c(-80,60))
axis(1, at=1:5, labels=c(1,2,3,4,5))
axis(2) 

barplot(dif96, main='1996', xlab='Employment Insecurity', ylab='Differences between Labour vs non-Labour Vote', axes=F, ylim=c(-80,60))
axis(1, at=1:5, labels=c(1,2,3,4,5))
axis(2) 

barplot(dif97, main='1997', xlab='Employment Insecurity', ylab='Differences between Labour vs non-Labour Vote', axes=F, ylim=c(-80,60))
axis(1, at=1:5, labels=c(1,2,3,4,5))
axis(2) 

detach(dist)

###############
#       Table 1       #
###############

## See Stata Code

###############
#       Figure 4      #
###############

# MNL simulation: Run Stata code first

par(mfrow=c(2,2))

m=c(0.375, 0.190)
se=c(0.333/34.47, 0.250/34.47)
bp=barplot(m, names=c("Employment good", "Employment bad"), ylim=c(0.05, 0.55), xpd=FALSE, main='Conservative Party')
box()
points(bp, pch=15, col="red")
arrows(x0=bp, y0=m-(1.96*se), y1=m+(1.96*se), code=3, angle=90)

m=c(0.361, 0.527)
se=c(0.328/34.47, 0.307/34.47)
cp=barplot(m, names=c("Employment good", "Employment bad"), ylim=c(0.05, 0.55), xpd=FALSE, main='Labour Party')
box()
points(cp, pch=15, col="red")
arrows(x0=cp, y0=m-(1.96*se), y1=m+(1.96*se), code=3, angle=90)

m=c(0.167, 0.190)
se=c(0.163/34.47, 0.159/34.47)
dp=barplot(m, names=c("Employment good", "Employment bad"), ylim=c(0.05, 0.55), xpd=FALSE, main='Liberal Democratic Party')
box()
points(dp, pch=15, col="red")
arrows(x0=dp, y0=m-(1.96*se), y1=m+(1.96*se), code=3, angle=90)

m=c(0.096, 0.094)
se=c(0.096/34.47, 0.096/34.47)
ep=barplot(m, names=c("Employment good", "Employment bad"), ylim=c(0.05, 0.55), xpd=FALSE, main='Abstention')
box()
points(ep, pch=15, col="red")
arrows(x0=ep, y0=m-(1.96*se), y1=m+(1.96*se), code=3, angle=90)

###############
#       Table 2       #
###############

## See Stata Code

###############
#       Table 3       #
###############

## See Stata Code

###############
#       Figure 5      #
###############

# Micro-evidence of preferences: Run Stata code first

par(mfrow=c(2,3))

bp=barplot(c(3.24, 2.39), names=c("Labour switchers", "Non-Labour voters"), ylim=c(2,4), main='Employment situation evaluations (92-97)', xpd=FALSE)
box()
arrows(x0=bp, y0=c(3.06, 2.31), y1=c(3.42, 2.47), code=3, angle=90)

cp=barplot(c(8.87, 7.71), names=c("Labour switchers", "Non-Labour voters"), ylim=c(7,10), main='Employment growth preferences (92-97)', xpd=FALSE)
box()
arrows(x0=cp, y0=c(8.56, 7.51), y1=c(9.18, 7.91), code=3, angle=90)

dp=barplot(c(8.63, 7.76), names=c("Labour switchers", "Non-Labour voters"), ylim=c(7,10), main='Tax and welfare spending (92-97)', xpd=FALSE)
box()
arrows(x0=dp, y0=c(8.41, 7.61), y1=c(8.87, 7.91), code=3, angle=90)

ep=barplot(c(2.60, 2.72), names=c("Labour switchers", "Non-Labour voters"), ylim=c(2,4), main='Employment situation evaluations (97-02)', xpd=FALSE)
box()
arrows(x0=ep, y0=c(2.45, 2.67), y1=c(2.77, 2.77), code=3, angle=90)

fp=barplot(c(9.97, 9.61), names=c("Labour switchers", "Non-Labour voters"), ylim=c(8,11), main='Employment growth preferences (97-02)', xpd=FALSE)
box()
arrows(x0=fp, y0=c(9.55, 9.45), y1=c(10.39, 9.76), code=3, angle=90)

gp=barplot(c(10.29, 9.82), names=c("Labour switchers", "Non-Labour voters"), ylim=c(8,11), main='Tax and welfare spending (97-02)', xpd=FALSE)
box()
arrows(x0=gp, y0=c(9.98, 9.69), y1=c(10.61, 9.95), code=3, angle=90)

###############
#       Table 4       #
###############

## See Stata Code

###############
#       Table 5       #
###############

## See Stata Code


#################
##    APPENDIX    ##
#################
	
###############
#      Figure A1     #
###############

# Micro-evidence 1997-2001: Run Stata code first

par(mfrow=c(1,3))

bp=barplot(c(2.73, 2.67), names=c("Switchers", "Labour voters"), ylim=c(2,4), main='Employment situation evaluations (97-02)', xpd=FALSE)
box()
arrows(x0=bp, y0=c(2.65, 2.59), y1=c(2.82, 2.75), code=3, angle=90)

cp=barplot(c(10.23, 10.70), names=c("Switchers", "Labour voters"), ylim=c(9,11), main='Labour: employment growth (97-02)', xpd=FALSE)
box()
arrows(x0=cp, y0=c(9.96, 10.52), y1=c(10.50, 10.88), code=3, angle=90)

dp=barplot(c(9.91, 10.24), names=c("Switchers", "Labour voters"), ylim=c(9,11), main='Labour: tax and welfare spending (97-02)', xpd=FALSE)
box()
arrows(x0=dp, y0=c(9.66, 10.08), y1=c(10.16, 10.40), code=3, angle=90)




